/**
 * Created by Administrator on 2015/5/24.
 */
// 实现与MySQL交互
var mysql = require('mysql');
var $conf = require('../conf/db');
var $util = require('../util/util');
var $sql = require('./userSqlMapping');

// 使用连接池，提升性能
var pool  = mysql.createPool($util.extend({}, $conf.mysql));
//var pool  = mysql.createPool($conf.mysql);

// 向前台返回JSON方法的简单封装
var jsonWrite = function (res, ret) {
    if(typeof ret === 'undefined') {
        res.json({
            code:'1',
            msg: '操作失败'
        });
    } else {
        res.json(ret);
    }
};

module.exports = {
    add: function (req, res, next,r) {
        pool.getConnection(function(err, connection) {
            // 获取前台页面传过来的参数
            var param = req.body;
            if(param.name == null || param.age == null) {
                jsonWrite(res, undefined);
                return;
            }
            console.log(param.name);
            connection.query($sql.insert, [param.name, param.age], function(err, result) {
                r(result);
                // 释放连接
                connection.release();
            });
        });
    },
    delete: function (req, res, next) {
        // delete by Id
        pool.getConnection(function(err, connection) {
            var id = +req.query.id;
            connection.query($sql.delete, id, function(err, result) {
                if(result.affectedRows > 0) {
                    result = {
                        code: 200,
                        msg:'删除成功'
                    };
                } else {
                    result = void 0;
                }
                jsonWrite(res, result);
                connection.release();
            });
        });
    },
    update: function (req, res, next,r) {
        // update by id
        // 为了简单，要求同时传name和age两个参数
        var param = req.body;
        if(param.name == null || param.age == null || param.id == null) {
            jsonWrite(res, undefined);
            return;
        }

        pool.getConnection(function(err, connection) {
            connection.query($sql.update, [param.name, param.age, +param.id], function(err, result) {
                r(result);
                connection.release();
            });
        });

    },
    queryById: function (req, res, next,r) {
        var id = +req.query.id; // 为了拼凑正确的sql语句，这里要转下整数
        pool.getConnection(function(err, connection) {
            connection.query($sql.queryById, id, function(err, result) {
                //jsonWrite(res, result);
                r(result[0]);
                connection.release();
            });
        });
    },
    queryAll: function (req, res, next,r) {
        var pageNum=parseInt(req.query.pageNum==null?1:req.query.pageNum);
        var pageSize=parseInt(req.query.pageSize==null?5:req.query.pageSize);
        var count=0;
        pool.getConnection(function(err, connection) {
            connection.query($sql.queryCount, function(err, result) {
                count=result[0].count;
            });

            //回调函数
            connection.query($sql.queryAll,[(pageNum-1)*pageSize,pageSize], function(err, result) {
                //回调
                //jsonWrite(res, result);
                var pageCount=0;
                if(count%pageSize==0){
                    pageCount=parseInt(count/pageSize);
                }else{
                    pageCount=parseInt(count/pageSize)+1;
                }
                r(result,pageNum,pageCount);
                connection.release();
            });
        });
    }

};